package at.ltasat.train.classifier;

import java.io.File;

import at.ltasat.Configuration;
import at.ltasat.classifier.knn.KNearestNeighbourClassifierFactory;

public final class TrainClassifierConfiguration extends Configuration {

	private String classifier;
	private File file;
	private boolean isVerboseConfig, isVerboseStatus;

	{
		classifier = KNearestNeighbourClassifierFactory.NAME;
		file = new File("classifier");
		isVerboseConfig = false;
		isVerboseStatus = false;
	}

	public static TrainClassifierConfiguration get(String[] args) {
		TrainClassifierConfiguration config = new TrainClassifierConfiguration();
		for (String arg : args) {
			if (arg.length() == 0) {
				continue;
			}
			if (arg.startsWith("-")) {
				String name = arg.substring(0, arg.indexOf('='));
				String value = arg.substring(arg.indexOf('=') + 1);
				if (name.equals("-v") || name.equals("--verbose")) {
					for (String vv : value.split(",")) {
						if (vv.equals("config")) {
							config.setVerboseConfig(true);
						} else if (vv.equals("status")) {
							config.setVerboseStatus(true);
						} else {
							throw new IllegalArgumentException("Illegal verbose mode: " + vv);
						}
					}
				} else if (name.equals("-c") || name.equals("--classifier")) {
					config.setClassifier(value);
				} else if (name.equals("-o") || name.equals("--out")) {
					config.setFile(new File(value));
				} else {
					throw new IllegalArgumentException("Unknown property name: " + name);
				}
			} else {
				config.setIn(new File(arg));
				break;
			}
		}
		return config;
	}

	public String getClassifier() {
		return classifier;
	}

	public File getFile() {
		return file;
	}

	public boolean isVerboseConfig() {
		return isVerboseConfig;
	}

	public boolean isVerboseStatus() {
		return isVerboseStatus;
	}

	public void setClassifier(String classifier) {
		assert classifier != null;
		this.classifier = classifier;
	}

	public void setFile(File file) {
		assert file != null;
		this.file = file;
	}

	public void setVerboseConfig(boolean isVerboseConfig) {
		this.isVerboseConfig = isVerboseConfig;
	}

	public void setVerboseStatus(boolean isVerboseStatus) {
		this.isVerboseStatus = isVerboseStatus;
	}

	@Override
	public String toString() {
		StringBuilder string = new StringBuilder();
		string.append(String.format("--verbose=%s,%s", isVerboseConfig ? "config" : "", isVerboseStatus ? "status" : ""));
		string.append(String.format("--classifier=%s ", classifier));
		string.append(String.format(file.getAbsolutePath()));
		return string.toString().trim();
	}

}
